package org.dromara.project.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.constant.SystemConstants;
import org.dromara.project.domain.vo.ProContractInfoVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.project.domain.vo.ProContractVo;
import org.dromara.project.domain.bo.ProContractBo;
import org.dromara.project.service.IProContractService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.service.ISysUserService;
import org.dromara.system.domain.vo.UserSimpleVo;


/**
 * 合同信息
 *
 * @author LUSH
 * @date 2025-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pro/contract")
public class ProContractController extends BaseController {

    private final IProContractService proContractService;
    private final ISysUserService sysUserService;

    /**
     * 查询合同信息列表
     */
    @SaCheckPermission("project:contract:list")
    @GetMapping("/list")
    public TableDataInfo<ProContractVo> list(ProContractBo bo, PageQuery pageQuery) {
        return proContractService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出合同信息列表
     */
    @SaCheckPermission("project:contract:export")
    @Log(title = "合同信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProContractBo bo, HttpServletResponse response) {
        List<ProContractVo> list = proContractService.queryList(bo);
        ExcelUtil.exportExcel(list, "合同信息", ProContractVo.class, response);
    }

    /**
     * 获取合同信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:contract:query")
    @GetMapping("/{id}")
    public R<ProContractInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(proContractService.queryInfoById(id));
    }

    /**
     * 新增合同信息
     */
    @SaCheckPermission("project:contract:add")
    @Log(title = "合同信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProContractBo bo) {
        return toAjax(proContractService.insertByBo(bo));
    }

    /**
     * 修改合同信息
     */
    @SaCheckPermission("project:contract:edit")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProContractBo bo) {
        return toAjax(proContractService.updateByBo(bo));
    }

    /**
     * 删除合同信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("project:contract:remove")
    @Log(title = "合同信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(proContractService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取项目执行总裁用户列表
     */
    @SaCheckPermission("project:contract:query")
    @GetMapping("/presidents")
    public R<List<UserSimpleVo>> getPresidents() {
        List<UserSimpleVo> list = sysUserService.selectUsersByRoleKey(SystemConstants.PROJECT_CEO_ROLE);
        return R.ok(list);
    }

}
